﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Serialization;

namespace GEAR_Structure
{
    [Serializable]
    public class Project
    {
        //private string filepath;

        private List<Category_material> materials;
        private List<Category_stratigraphy> stratigraphy;
        
        [XmlIgnore]
        private ClimateData.ClimateData climatedata;    //Il campo non viene serializzato


        public ClimateData.ClimateData Climatedata
        {
            get { return climatedata; }
        }

        public List<Category_stratigraphy> Stratigraphys
        {
            get { return stratigraphy; }
            set { stratigraphy = value; }
        }

        public List<Category_material> Materials
        {
            get { return materials; }
            set { materials = value; }
        }

        public Project()
        {
            this.materials = new List<Category_material>();
            this.stratigraphy = new List<Category_stratigraphy>();
            this.climatedata = ClimateData.ClimateData.load();
            this.loadMaterial(ApplicationInfo.materialPath);
        }

        /// <summary>
        /// Carica il progetto da una directory all'apertura
        /// </summary>
        /// <param name="path">path di recupero dati</param>
        /// <returns>restituisce l'oggetto deserializzatto</returns>
        public Project loadDirectory(string path)
        {
            Project ret = new Project();
            path += "\\data.xml";
            try
            {
                System.IO.StreamReader stream = new System.IO.StreamReader(path);
                System.Xml.Serialization.XmlSerializer x = new System.Xml.Serialization.XmlSerializer(typeof(Project));
                ret = (Project)x.Deserialize(stream);
                stream.Close();
                return ret;
            }
            catch
            {
                return ret;
            }
        }

        /// <summary>
        /// Carica il materiale da una serializzazione XML
        /// </summary>
        /// <param name="path">percorso del file xml</param>
        public void loadMaterial(string path)
        {
            List<Category_material> ret = new List<Category_material>();
            try
            {
                System.IO.StreamReader stream = new System.IO.StreamReader(path);
                System.Xml.Serialization.XmlSerializer x = new System.Xml.Serialization.XmlSerializer(typeof(List<Category_material>));
                ret = (List<Category_material>)x.Deserialize(stream);
                stream.Close();
                this.materials = ret;
            }
            catch
            {
            }
        }

        /// <summary>
        /// salva il progetto
        /// </summary>
        /// <param name="directoryName">percorso di salvataggio</param>
        public void export(string filename)
        {
            System.IO.StreamWriter stream = new System.IO.StreamWriter(filename);
            System.Xml.Serialization.XmlSerializer x = new System.Xml.Serialization.XmlSerializer(typeof(Project));
            x.Serialize(stream, this);
            stream.Close();
        }

        /// <summary>
        /// salva il progetto
        /// </summary>
        /// <param name="directoryName">percorso di salvataggio</param>
        public void save(string directorypath)
        {
            System.IO.StreamWriter stream = new System.IO.StreamWriter(directorypath + "\\" + Configuration.getInstance().DefaultName);
            System.Xml.Serialization.XmlSerializer x = new System.Xml.Serialization.XmlSerializer(typeof(Project));
            x.Serialize(stream, this);
            stream.Close();
        }

        /// <summary>
        /// salva solo le strutture
        /// </summary>
        /// <param name="filename">percorso di salvataggio</param>
        public void exportStratigraphy(string filename)
        {
            System.IO.StreamWriter stream = new System.IO.StreamWriter(filename);
            System.Xml.Serialization.XmlSerializer x = new System.Xml.Serialization.XmlSerializer(typeof(List<Category_stratigraphy>));
            x.Serialize(stream, this.stratigraphy);
            stream.Close();
        }

        public void importStratigraphy(string filename)
        {
            List<Category_stratigraphy> str = null;
            try
            {
                System.IO.StreamReader stream = new System.IO.StreamReader(filename);
                System.Xml.Serialization.XmlSerializer x = new System.Xml.Serialization.XmlSerializer(typeof(List<Category_stratigraphy>));
                str = (List<Category_stratigraphy>)x.Deserialize(stream);
                stream.Close();
                foreach (Category_stratigraphy cs in str)
                    this.stratigraphy.Add(cs);
            }
            catch
            {

            }
        }

    }
}
